package com.camel.docker.controller;

import com.baomidou.mybatisplus.service.IService;
import com.camel.core.controller.BaseCommonController;
import com.camel.core.entity.Result;
import com.camel.core.utils.ResultUtil;
import com.camel.docker.entity.Project;
import com.camel.docker.entity.search.ContainerAddForm;
import com.camel.docker.service.ContainerCreatorService;
import com.camel.docker.service.ContainerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/project")
@RestController
public class ProjectController extends BaseCommonController {

    @Autowired
    private ContainerService service;

    @Autowired
    private ContainerCreatorService containerCreatorService;

    @GetMapping
    public Result index(Project container) {
        return ResultUtil.success(service.selectPage(container));
    }

    /**
     * 创建指定容器
     *
     * @param containerAddForm
     * @return
     */
    @PostMapping
    public Result save(@RequestBody ContainerAddForm containerAddForm) {
        Project container = containerAddForm.loadContainer();
        service.insert(container);
        return ResultUtil.success("创建容器成功");
    }

    @PutMapping
    public Result edit(@RequestBody ContainerAddForm entity) {
        Project container = entity.loadContainer();
        return super.update(container);
    }

    @PostMapping("/add/{id}")
    public com.camel.docker.entity.Result add(@PathVariable Integer id) {
        Project container = service.selectById(id);
        ContainerAddForm containerAddForm = new ContainerAddForm(container);
        return containerCreatorService.creatFromGit(containerAddForm);
    }


    @DeleteMapping("/{id}")
    public Result del(@PathVariable Integer id) {
        return super.delete(id);
    }

    @GetMapping("/{id}")
    public Result details(@PathVariable Integer id) {
        return super.details(id);
    }

    @Override
    public IService getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "项目";
    }
}
